# Consistency checks for the build configuration
{%-  import 'consistency_checks.jinja' as consistency_checks -%}

# Verify signature verification configuration
{{ consistency_checks.suit_verify_signature_check("APP_LOCAL_3", app_recovery_img, sysbuild) }}

{%- set mpi_app_recovery_local_vendor_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_APP_LOCAL_3_VENDOR_NAME']|default('nordicsemi.com') %}
{%- set mpi_app_recovery_local_class_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_APP_LOCAL_3_CLASS_NAME']|default('nRF54H20_sample_app_3') %}
SUIT_Envelope_Tagged:
  suit-authentication-wrapper:
    SuitDigest:
      suit-digest-algorithm-id: cose-alg-sha-256
  suit-manifest:
    suit-manifest-version: 1
{%- if APP_LOCAL_3_SEQ_NUM is defined %}
    suit-manifest-sequence-number: {{ APP_LOCAL_3_SEQ_NUM }}
{%- elif DEFAULT_SEQ_NUM is defined %}
    suit-manifest-sequence-number: {{ DEFAULT_SEQ_NUM }}
{%- else %}
    suit-manifest-sequence-number: 1
{%- endif %}
    suit-common:
      suit-components:
      - - MEM
        - {{ app_recovery_img['dt'].label2node['cpu'].unit_addr }}
        - {{ get_absolute_address(app_recovery_img['dt'].chosen_nodes['zephyr,code-partition']) }}
        - {{ app_recovery_img['dt'].chosen_nodes['zephyr,code-partition'].regs[0].size }}
      - - CAND_IMG
        - 0
      suit-shared-sequence:
      - suit-directive-set-component-index: 0
      - suit-directive-override-parameters:
          suit-parameter-vendor-identifier:
            RFC4122_UUID: {{ mpi_app_recovery_local_vendor_name }}
          suit-parameter-class-identifier:
            RFC4122_UUID:
              namespace: {{ mpi_app_recovery_local_vendor_name }}
              name: {{ mpi_app_recovery_local_class_name }}
          suit-parameter-image-digest:
            suit-digest-algorithm-id: cose-alg-sha-256
            suit-digest-bytes:
              file: {{ app_recovery_img['binary'] }}
          suit-parameter-image-size:
            file: {{ app_recovery_img['binary'] }}
      - suit-condition-vendor-identifier:
        - suit-send-record-success
        - suit-send-record-failure
        - suit-send-sysinfo-success
        - suit-send-sysinfo-failure
      - suit-condition-class-identifier:
        - suit-send-record-success
        - suit-send-record-failure
        - suit-send-sysinfo-success
        - suit-send-sysinfo-failure
    suit-validate:
    - suit-directive-set-component-index: 0
    # In the case of streaming operations it is worth to retry them at least once.
    # This increases the robustness against bit flips on the transport,
    # for example when storing the data on an external memory device.
    # The suit-directive-try-each will complete on the first successful subsequence.
    - suit-directive-try-each:
      - - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
      - - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
    suit-invoke:
    - suit-directive-set-component-index: 0
    - suit-directive-run-sequence:
{%- if 'CONFIG_SUIT_INVOKE_APP_LOCAL_3_BEFORE_MAIN_APP' in app_recovery_img['config'] and app_recovery_img['config'][CONFIG_SUIT_INVOKE_APP_LOCAL_3_BEFORE_MAIN_APP] != ''  %}
      - suit-directive-override-parameters:
          suit-parameter-invoke-args:
            suit-synchronous-invoke: True
            suit-timeout: 5000
          suit-parameter-soft-failure: True
{%- endif %}
      - suit-directive-invoke:
        - suit-send-record-failure

{%- if APP_LOCAL_3_VERSION is defined %}
    suit-current-version: {{ APP_LOCAL_3_VERSION }}
{%- elif DEFAULT_VERSION is defined %}
    suit-current-version: {{ DEFAULT_VERSION }}
{%- endif %}

    suit-install:
    - suit-directive-set-component-index: 1
    - suit-directive-override-parameters:
{%- if 'CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI' in app_recovery_img['config'] and app_recovery_img['config']['CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI'] != '' %}
        suit-parameter-uri: '{{ app_recovery_img['config']['CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI'] }}'
{%- else %}
        suit-parameter-uri: '#{{ app_recovery_img['name'] }}'
{%- endif %}
        suit-parameter-image-digest:
          suit-digest-algorithm-id: cose-alg-sha-256
          suit-digest-bytes:
            file: {{ app_recovery_img['binary'] }}
    - suit-directive-fetch:
      - suit-send-record-failure
    - suit-directive-try-each:
      - - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
      - - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
    - suit-directive-set-component-index: 0
    - suit-directive-override-parameters:
        suit-parameter-source-component: 1
    # When copying the data it is worth to retry the sequence of
    # suit-directive-copy and suit-condition-image-match at least once.
    # If a bit flip occurs, it might be due to a transport issue, not
    # a corrupted candidate image. In this case the bit flip is recoverable
    # and it is worth retrying the operation.
    # The suit-directive-try-each will complete on the first successful subsequence.
    - suit-directive-try-each:
      - - suit-directive-copy:
          - suit-send-record-failure
        - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
      - - suit-directive-copy:
          - suit-send-record-failure
        - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
    suit-text:
      suit-digest-algorithm-id: cose-alg-sha-256

    suit-candidate-verification:
    - suit-directive-set-component-index: 1
    - suit-directive-override-parameters:
{%- if 'CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI' in app_recovery_img['config'] and app_recovery_img['config']['CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI'] != '' %}
        suit-parameter-uri: '{{ app_recovery_img['config']['CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI'] }}'
{%- else %}
        suit-parameter-uri: '#{{ app_recovery_img['name'] }}'
{%- endif %}
        suit-parameter-image-digest:
          suit-digest-algorithm-id: cose-alg-sha-256
          suit-digest-bytes:
            file: {{ app_recovery_img['binary'] }}
    - suit-directive-fetch:
      - suit-send-record-failure
    - suit-directive-try-each:
      - - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure
      - - suit-condition-image-match:
          - suit-send-record-success
          - suit-send-record-failure
          - suit-send-sysinfo-success
          - suit-send-sysinfo-failure

    suit-manifest-component-id:
    - INSTLD_MFST
    - RFC4122_UUID:
        namespace: {{ mpi_app_recovery_local_vendor_name }}
        name: {{ mpi_app_recovery_local_class_name }}
  suit-text:
    en:
      '["MEM", {{ app_recovery_img['dt'].label2node['cpu'].unit_addr }}, {{ get_absolute_address(app_recovery_img['dt'].chosen_nodes['zephyr,code-partition']) }}, {{ app_recovery_img['dt'].chosen_nodes['zephyr,code-partition'].regs[0].size }}]':
        suit-text-vendor-name: Nordic Semiconductor ASA
        suit-text-model-name: nRF54H20_cpuapp_3
        suit-text-vendor-domain: nordicsemi.com
        suit-text-model-info: The nRF54H20 application core companion/recovery
        suit-text-component-description: Sample application core companion/recovery FW
        suit-text-component-version: v1.0.0
{%- if 'CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE' not in app_recovery_img['config'] or app_recovery_img['config']['CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE'] == '' %}
  suit-integrated-payloads:
    '#{{ app_recovery_img['name'] }}': {{ app_recovery_img['binary'] }}
{%- endif %}
